#include <stdio.h>
#include <assert.h>
#include "papers.h"


void test_papers_crud()
{
  struct papers_t papers;

  papers_create(&papers);
  assert(papers_count(&papers) == 0);
  int id1 = papers_add(&papers, "Marek", "Superboss", "filename.ext");
  assert(papers_count(&papers) == 1);
  assert(strcmp(get_paper_by_id(&papers, id1)->author, "Marek") == 0);
  int id2 = papers_add(&papers, "Darek", "Tez boss", "filename.ext");
  assert(papers_count(&papers) == 2);
  assert(strcmp(get_paper_by_id(&papers, id2)->author, "Darek") == 0);
  assert(strcmp(get_paper_by_id(&papers, id2)->title, "Tez boss") == 0);
}

void test_upload()
{
  struct papers_t papers;
  papers_create(&papers);
  int id1 = papers_add(&papers, "Marek", "Superboss", "filename.ext");
  assert(papers_count(&papers) == 1);
  
  struct paper_t * paper; 
  paper = get_paper_by_id(&papers, id1);
  assert(paper->size == 0);
  paper_add_block(paper, "ala", 3);
  assert(paper->size == 3);
  paper_add_block(paper, " ma ", 4);
  assert(paper->size == 7);
  paper_add_block(paper, "kota", 4);
  assert(paper->size == 11);
  assert(strncmp("ala ma kota", paper->data ,11) == 0);
}

void test_download_simple()
{
  struct paper_t * paper;
  struct papers_t papers;
  
  papers_create(&papers);
  int id1 = papers_add(&papers, "Marek", "Superboss", "filename.ext");
  paper = get_paper_by_id(&papers, id1);
  assert(paper != 0);
  paper_add_block(paper, "ala", 3);
  assert(paper->size == 3);

  int size;
  char block[BLOCK_SIZE];
  int i = 0;

  size = paper_get_block(paper, i++, block);
  assert(size == 3);
  assert(strncmp(block,"ala",3) == 0);
  size = paper_get_block(paper, i++, block);
  assert(size == -1);
}

void generate_block(char * block, char one, char two)
{
  int i;
  for (i = 0; i < BLOCK_SIZE/2; i++) {
    block[i] = one;
    block[BLOCK_SIZE/2+i] = two;
  }
}

void test_blocks()
{
  struct paper_t * paper;
  struct papers_t papers;
  
  papers_create(&papers);
  int id1 = papers_add(&papers, "Marek", "Superboss", "filename.ext");
  paper = get_paper_by_id(&papers, id1);
  
  char block[BLOCK_SIZE];
  generate_block(block, 'a', 'b');
  paper_add_block(paper, block, BLOCK_SIZE);
  generate_block(block, 'c', 'd');
  paper_add_block(paper, block, BLOCK_SIZE);
  paper_add_block(paper, "e", 1);

  int size, i, j = 0;
  for (i = 0;i < BLOCK_SIZE/2; i++, j++)
    assert(paper->data[j] == 'a');
  for (i = 0;i < BLOCK_SIZE/2; i++, j++)
    assert(paper->data[j] == 'b');
  for (i = 0;i < BLOCK_SIZE/2; i++, j++)
    assert(paper->data[j] == 'c');
  for (i = 0;i < BLOCK_SIZE/2; i++, j++)
    assert(paper->data[j] == 'd');
  assert(paper->data[j] == 'e');

  j = 0;
  size = paper_get_block(paper, 0, block);
  assert(size == BLOCK_SIZE);
  for (i = 0;i < BLOCK_SIZE/2; i++, j++)
    assert(block[j] == 'a');
  for (i = 0;i < BLOCK_SIZE/2; i++, j++)
    assert(block[j] == 'b');

  j = 0;
  size = paper_get_block(paper, 1, block);  
  assert(size == BLOCK_SIZE);
  
  for (i = 0;i < BLOCK_SIZE/2; i++, j++) 
    assert(block[j] == 'c');
  for (i = 0;i < BLOCK_SIZE/2; i++, j++)
    assert(block[j] == 'd');

  size = paper_get_block(paper, 2, block);    
  assert(size == 1);
  assert(*block == 'e');

}

int main() 
{
  test_papers_crud();
  test_upload();
  test_download_simple();
  test_blocks();
  printf("Test: ok\n");
  return 0;
}
